<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/10/25
 * Time: 17:23
 */

namespace App\Plugin\HostServer\Front;


use App\Plugin\HostServer\Front\Mail\EmailCode;
use App\Plugin\HostServer\Models\PnHostSvCategory;
use App\Plugin\HostServer\Models\PnHostSvCode;
use App\Plugin\HostServer\Models\PnHostSvEmailLog;
use App\Plugin\HostServer\Models\PnHostSvSystemGoods;
use App\TraitClass\CheckFormTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;

class ApiController extends BaseController
{
    use CheckFormTrait;

    public function emailCode(Request $request){
        $data = $request->all();
        $check_data = [
            'email' => ['required', 'email']
        ];

        $error = $this->checkForm($data, $check_data, [], ['email' => '邮箱账户']);
        if (count($error) > 0) {
            $errors = $this->checkFormErrorFormat($error);
            return $this->returnErrorApi($errors['msg']);
        }
        try{
            $r=PnHostSvCode::sendEmail($data['email']);
            if($r)
            {

                Mail::to($data['email'])->send(new EmailCode($r['code']));
                return $this->returnOkApi('发送成功');
            }
            return $this->returnErrorApi('发送失败');
        }catch (\Exception $exception)
        {
            return $this->returnErrorApi('发送失败'.$exception->getMessage());
        }

    }

    /**
     * 取得产品系统版本
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function system(Request $request){
        $id=$request->input('id');
        $good_id=$request->input('good_id');

        if(empty($id) )
        {
            return $this->returnErrorApi('缺少参数');
        }
        if(empty($good_id) )
        {
            return $this->returnErrorApi('缺少参数');
        }
        $system_ids=PnHostSvSystemGoods::where('category_id',$id)->where('good_id',$good_id)->get()->pluck('sub_id')->toArray();
        if(empty($system_ids)){
            return $this->returnErrorApi('缺少参数2');
        }
        $systems = PnHostSvCategory::where('group_type', 'system')->whereIn('id', $system_ids)->get()->toArray();
        if(empty($systems))
        {
            return $this->returnErrorApi('缺少参数3');
        }
        return $this->returnOkApi('获取成功',$systems);
    }



}